package web.demo.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import web.demo.entity.FoodTable;
import web.demo.entity.FoodType;
import web.demo.factory.BeanFactory;
import web.demo.service.impl.FoodTypeService;

public class FoodTypeServlet extends BaseServlet {


	protected Object foodTypeList(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//获取全部菜系信息
		List<FoodType> list=foodTypeService.getAll();
		req.setAttribute("foodTypeList",list);
		
		return req.getRequestDispatcher("sys/foodType/foodTypeList.jsp");
	}

	protected Object saveFoodType(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name=req.getParameter("name");
		FoodType foodType=new FoodType();
		foodType.setName(name);
		boolean result=foodTypeService.addFoodType(foodType);
		if(result){
			//添加成功
			return req.getRequestDispatcher("FoodTypeServlet?method=foodTypeList");
		}else{
			//添加失败
			return "error/error.jsp";
		}
	}

	protected Object getById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id=request.getParameter("id");
		FoodType foodType=foodTypeService.getById(id);
		request.setAttribute("foodType",foodType);
		
		return request.getRequestDispatcher("sys/foodType/updateFoodType.jsp");
	}
	
	protected Object update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id=request.getParameter("id");
		String name=request.getParameter("name");
		FoodType foodType=new FoodType();
		int foodId=Integer.parseInt(id);
		foodType.setId(foodId);
		foodType.setName(name);
		foodTypeService.update(foodType);
		
		return request.getRequestDispatcher("FoodTypeServlet?method=foodTypeList");
	}
	//删除菜系
	protected Object delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id=request.getParameter("id");
		String appPath=request.getServletContext().getRealPath("app/detail/style/images/");
		String sysPath=request.getServletContext().getRealPath("sys/style/images/");
		boolean result=foodTypeService.delete(id,appPath,sysPath);
		if(result){
			return request.getRequestDispatcher("FoodTypeServlet?method=foodTypeList");
		}else{
			return "error/error.jsp";
		}
	}
	//搜索菜系
	public Object search(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String selectCondition="";
		if(request.getParameter("selectCondition")!=null){
			selectCondition=new String(request.getParameter("selectCondition").getBytes("ISO-8859-1"),"utf-8");
		}
		List<FoodType> foodTypeList=foodTypeService.select(selectCondition);
		request.setAttribute("foodTypeList",foodTypeList);
		
		return request.getRequestDispatcher("sys/foodType/foodTypeList.jsp");
	}
	
}
